package com.titar.robot.ui.welcom;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.titar.robot.R;
import com.titar.robot.RobotApp;
import com.titar.robot.base.BaseActivity;
import com.titar.robot.ui.login.LoginActivity;
import com.titar.robot.ui.main.MainActivity;
import com.titar.robot.utils.PermissionUtils;
import com.titar.robot.utils.SharedPreUtils;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModel;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class WelcomeActivity extends BaseActivity<WelcomeContact.Presenter> implements WelcomeContact.View {
    private ViewPager welcom_vp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RobotApp.setIsLife(true);
    }

    @Override
    protected void onInitWidget(Bundle savedInstanceState) {
        mPresenter = new WelcomePresenter().attachView(this);
        getLifecycle().addObserver(mPresenter);
        welcom_vp = findViewById(R.id.welcom_vp);
        if (Build.VERSION.SDK_INT < 23 || permissions.length == 0) {
            initView();
        } else {
            requestPermissions();
        }
    }

    @Override
    protected void onInitListener() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.layout_welcom_activity;
    }


    private void initView() {
        if (SharedPreUtils.getInstence(WelcomeActivity.this).isFirst()) {
            showGuidVp();
        } else {
            String token = SharedPreUtils.getInstence(WelcomeActivity.this).getToken();
            if (token.isEmpty()) {
                jump2ActivityAndFinish(LoginActivity.class);
            } else {
                //检查token是否过期
                mPresenter.checkToken(token);
            }
        }
    }

    @Override
    protected void onDestroy() {
        mPresenter.detachView();
        super.onDestroy();
    }


    ////////////////////////////////////////////////////////////////////////////
    //////////////////////////////-----权限请求--------/////////////////////////
    ///////////////////////////////////////////////////////////////////////////
    private boolean mPermissionreadWriteExtendStorage = false;


    @Override
    public void onPermissionGranted(String permission) {
        super.onPermissionGranted(permission);
        updatePermission(permission, true);
        boolean isNotify = PermissionUtils.isNotificationEnabled(this);
        if (allPermissioinsGranted()) {
            if (isNotify) {
                initView();
            } else {
                initView();
            }
        }
    }

    /*获取权限被拒绝*/
    @Override
    public void onPermissionDenied(String permission) {
        super.onPermissionDenied(permission);
        updatePermission(permission, false);
    }


    /*权限没拿到，用户不去设置权限*/
    @Override
    protected void onClcikSettingPermissionCancel() {
        //退出应用
        RobotApp.appExit();
    }

    @Override
    protected void onClcikSettingPermissionCancelNotification() {
        super.onClcikSettingPermissionCancelNotification();
        initView();
    }

    /*从设置界面返回*/
    @Override
    public void onRecheckPermission() {
        super.onRecheckPermission();
        requestPermissions();
    }


    private void updatePermission(String permission, boolean granted) {
        switch (permission) {
            case Manifest.permission.WRITE_EXTERNAL_STORAGE:
                mPermissionreadWriteExtendStorage = granted;
                break;
        }
    }

    String[] permissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    };

    private void requestPermissions() {
        Log.i("DeniedList", "checkPermissions");
        checkPermissions(permissions);
    }

    private boolean allPermissioinsGranted() {
        return mPermissionreadWriteExtendStorage;
    }

    ////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////


    private void showGuidVp() {
        welcom_vp.setAdapter(new GuideAdapter());
    }

    @Override
    public void onTokenResult(boolean b) {
        if (b) {
            jump2ActivityAndFinish(MainActivity.class);
        } else {
            jump2ActivityAndFinish(LoginActivity.class);
        }

    }

    private class GuideAdapter extends PagerAdapter {
        private Object[][] imageSourceId = new Object[][]{
                {getResources().getString(R.string.robot)
                        , String.format("%s%s%s", getResources().getString(R.string.robot), getResources().getString(R.string.robot), getResources().getString(R.string.robot))
                        , R.mipmap.guid1},
                {getResources().getString(R.string.robot)
                        , String.format("%s%s", getResources().getString(R.string.robot), getResources().getString(R.string.robot))
                        , R.mipmap.guid1},
                {getResources().getString(R.string.robot)
                        , ""
                        , R.mipmap.guid1},
        };

        @Override
        public int getCount() {
            return imageSourceId.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View itemView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.itemt_guide_viewpager, container, false);
            View imageView = itemView.findViewById(R.id.image_view);
            View btnJoin = itemView.findViewById(R.id.btn_complete);
            TextView guide_title = (TextView) itemView.findViewById(R.id.guide_title);
            TextView guide_content = (TextView) itemView.findViewById(R.id.guide_content);
            TextView guide_join_content = (TextView) itemView.findViewById(R.id.guide_join_content);
            imageView.setBackgroundResource((Integer) imageSourceId[position][2]);
            if (position == getCount() - 1) {
                guide_title.setVisibility(View.GONE);
                guide_content.setVisibility(View.GONE);
                guide_join_content.setVisibility(View.VISIBLE);
                btnJoin.setVisibility(View.VISIBLE);
                btnJoin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        jump2ActivityAndFinish(LoginActivity.class);
                        finish();
                        SharedPreUtils.getInstence(WelcomeActivity.this).setIsFirst();
                    }
                });
            } else {
                guide_join_content.setVisibility(View.GONE);
                btnJoin.setVisibility(View.GONE);
                guide_title.setVisibility(View.VISIBLE);
                guide_content.setVisibility(View.VISIBLE);
                //  guide_title.setText((String) imageSourceId[position][0]);
                //  guide_content.setText((String) imageSourceId[position][1]);
            }
            container.addView(itemView);
            return itemView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
